<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------

/**
 * 文件上传
 * @name    Upload
 * @see  
 * @version	1.0.0 (Mon Nov 24 15:35:13 CST 2008)
 * @author	sanshi
 */
 
class Upload
{
	//设置文件上传的最大量 单位是k
	var $uploadFileFize = 2000;
	var $tempFileName = array();
	var $uploadFileName = array();
	var $allowFile = array();
	var $io = "";
	var $isDir = false;
   /**
	* 构造函数
	* @return void 
	* @access public
	*/
	public function __construct()
	{
		$this->Upload();
	}
	function Upload()
	{
		$this->io = Factory::getBaseClass("FileIO");
		//计算最大上传数量
		$this->uploadFileFize = $this->uploadFileFize*1024;
		//设置同意上传类型
		$this->allowFile = array('txt','jpg','jpeg','bmp','png','rar','zip','doc','xls','ppt','pdf','vsd','wps','et','dps');
		
	}
	function set()
	{
		
	}
	/**
	 * 初步过滤
	 * @param  $fileField     上传的字段名
	 * */
	function init($fileField)
	{
		//如果
		if(!isset($_FILES[$fileField])) return true;
		$files    = $_FILES[$fileField];
		//循环检测
		foreach($files['name'] as $k=>$v)
		{
			$fileName = $v;
			if($fileName=="") continue;
			$this->uploadFileName[$k] = $fileName;
			$fileSize = $files['size'][$k];   //文件大小
			//取得文件类型
			$uploadFileType  = $this->io->getFileType($fileName);
			if(!in_array($uploadFileType,$this->allowFile))
			{
				//上传类型不匹配
				alertMsg('-2020');
				return false;
			}
			if($fileSize > $this->uploadFileFize)
			{
				//文件超大
				alertMsg('-2021');
				return false;
			}
			$fileTemp = $files['tmp_name'][$k]; //临时文件名
			$this->tempFileName[$k] = $fileTemp ;
		}
		return true;
	}
	/**
	 * 真是上传动作
	 * @param  $upLoadDir    上传路径
	 * */
	function exec_upload($upLoadDir,$file_no,$pid)
	{
		//判断上传文件给的路径是否有结束符号
		if(substr($upLoadDir,0,-1)!="/") $upLoadDir.'/';
		//判断目标文件是否为空
		if(empty($this->tempFileName)) return false;
		foreach($this->tempFileName as $k=>$v)
		{
			$uploadFileType = $this->io->getFileType($this->uploadFileName[$k]);
			$file_no_temp = $file_no;
			$file_name = isset($file_name) ? $file_name : '';
			$file_name .="{$file_no_temp}.".$uploadFileType;
			$file_name = $this->getUploadFileName($file_name,$pid);
			$file_name = $upLoadDir.$file_name;
			if($this->io->copyFile($this->tempFileName[$k],$file_name))
			{
				$suc_file[] = $file_name;
			}
			unset($file_name);
		}
		//print_r($suc_file);exit();
		return $suc_file;
	}
	//取得上传文件名
	function getUploadFileName($uploadFile,$pid)
	{
		list($usec, $sec) = explode(" ", microtime());
		$name = mt_rand(0,$sec)."_{$pid}_";
		if($this->isDir)
			$name .= date('Y-m/').date("Y,m,d,H,i,s")."_".$this->chinaEncode($uploadFile);
		else
			$name .= date("Y,m,d,H,i,s")."_".$this->chinaEncode($uploadFile);
		return $name;
	}
	//中文编码
	function chinaEncode($str)
	{
		$len = strlen($str);
		$temp = "";
		for ($i=0;$i<$len;$i++)
		{
			$code = substr($str,$i,1);
			$chr = ord($code);
			if($chr>=161 && $chr<=255)
			{
				$temp .= 'c_'.$chr."_c";
			}else{
				$temp .= $code;
			}	
		}
		return $temp;
	}
}
?>